<?
require_once("Database.php");
class Cache {
	private $lockfile = "./cache/proc.lck";
	private static $proc = 0;
	
	function Cache() {
		
	}
	
	function getInstance() {
        static $instance;
		if(!isset($instance)) {
            $instance = new Cache();
		}
        return $instance;
    }

	function startCaching() {
		// if lock file does not exists or is older than 1 hour
		if(!file_exists($this->lockfile) || Utility::fileLifetime($this->lockfile) > 3600) {
			if (Utility::isWindows()) {
			    // Assume windows environment
			    $out = exec("php cacheproc.php");
			} else {
			    // Assume unix environment
				$out = exec("/usr/local/bin/php cacheproc.php > /dev/null &");
				$fp = fopen($this->lockfile, "w");
				fclose($fp);
			}
		}
		$_SESSION[$_GET['location']] = 1;
        return $out;
    }
	
    function queueSimilarArtists($name) {
        $db = Database::getInstance();
        $db->query("INSERT INTO cachequeue(name,type,timestamp) VALUES('".$name."', 'artist.getsimilar', now())", null, false);
	}
	
	function queueEvents($location) {
		$db = Database::getInstance();
        $db->query("INSERT INTO cachequeue(name,type,timestamp) VALUES('".$location."', 'geo.getevents', now())", null, false);
	}
	
	/*
	function queueArtistInfo($name) {
        $db = Database::getInstance($name);
        $db->query("INSERT INTO cachequeue(artistname,type,timestamp) VALUES('".$name."', 'artist.getinfo', now())", null, false);
	} 
    */
	
}
?>